package cn.cloud.all.security.web.header.writers;

import cn.cloud.all.security.web.header.HeaderWriter;
import cn.cloud.all.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Delegates to the provided {@link HeaderWriter} when
 * {@link RequestMatcher#matches(HttpServletRequest)} returns true.
 */
public final class DelegatingRequestMatcherHeaderWriter implements HeaderWriter {
    private final RequestMatcher requestMatcher;

    private final HeaderWriter delegateHeaderWriter;

    /**
     * Creates a new instance
     *
     * @param requestMatcher       the {@link RequestMatcher} to use. If returns true, the
     *                             delegateHeaderWriter will be invoked.
     * @param delegateHeaderWriter the {@link HeaderWriter} to invoke if the
     *                             {@link RequestMatcher} returns true.
     */
    public DelegatingRequestMatcherHeaderWriter(RequestMatcher requestMatcher, HeaderWriter delegateHeaderWriter) {
        Assert.notNull(requestMatcher, "requestMatcher cannot be null");
        Assert.notNull(delegateHeaderWriter, "delegateHeaderWriter cannot be null");
        this.requestMatcher = requestMatcher;
        this.delegateHeaderWriter = delegateHeaderWriter;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.security.web.headers.HeaderWriter#writeHeaders(javax.servlet
     * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
        if (this.requestMatcher.matches(request)) {
            this.delegateHeaderWriter.writeHeaders(request, response);
        }
    }

    @Override
    public String toString() {
        return getClass().getName() + " [requestMatcher=" + this.requestMatcher + ", delegateHeaderWriter=" + this.delegateHeaderWriter + "]";
    }
}
